package dcpb.re.encryption;


import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Field;
import it.unisa.dia.gas.jpbc.Pairing;
import it.unisa.dia.gas.plaf.jpbc.field.curve.CurveElement;
import it.unisa.dia.gas.plaf.jpbc.field.z.ZrElement;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;
import umbral.Capsule;

import java.io.IOException;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.AbstractMap.SimpleEntry;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class Demo {
	public static long time1=0;
	public static long time2=0;
	public static List<Element> P;
	public static List<Element> revoke;
	public static void main(String[] args) {
//		ProxyReEncrypt  d=new ProxyReEncrypt ();
//		SimpleEntry<PubParams,Element> retMap=d.setup();//CA 生成系统参数
//		PubParams p=retMap.getKey();
//		List l=d.getSecret2(new BigInteger("175"),4,p);
//		BigInteger b=d.recover2((BigInteger[])l.get(1),(BigInteger[])l.get(0),3);
//		System.out.println(b);
		int k=3,n=4,m0=3;//public
		//5个互质素数

		int[] m={11,13,17,19};//public

		int S=7,a=51,first=S+a*m0;
		int[] s=new int[m.length];
		for(int i=0;i<m.length;i++){//有n个秘密
			s[i]=first%m[i];
		}


		int M=1;
		for(int i=0;i<k;i++){//取前三个
			M=M*m[i];
		}
		int[] e=new int[m.length];
		for(int i=0;i<k;i++){
			e[i]=s[i]*M/m[i];
		}
		int x0=0;
		for(int i=0;i<k;i++){
			x0+=s[i]*e[i];
		}
		System.out.print(x0%(M)%m0);

	}
	void main2(){
		ProxyReEncrypt  d=new ProxyReEncrypt ();
		int m=5,t=3;
		SimpleEntry<PubParams,Element> retMap=d.setup();//CA 生成系统参数
		PubParams p=retMap.getKey();
		//d.save(p);
		Fabric f=new Fabric(p,m,t,retMap.getValue());
		//左对齐 一共128位，将右边空的补零
		//System.out.println(String.format("%-128s", "686173685F746F5F6375727665626E").replace(' ', '0'));


		byte[] message = null;// 要发送的信息
		String ms="hello world";
		message=ms.getBytes();//d.readFile(filePath1);
		System.out.println("发送的信息 ： "+ms);

		int[] numbers={5};
		for(int j=0;j<numbers.length;j++) {
			long startTime = System.currentTimeMillis();    //获取开始时间
			int reveiveNumber=numbers[j];
			List<Bob> receiver = new LinkedList<Bob>();
			for (int i = 0; i < reveiveNumber; i++) {
				receiver.add(new Bob(p, t, f.KeyGen()));
			}

			Alice a = new Alice(p, m, t);

			revoke = new LinkedList<Element>();

			revoke.add(new Bob(p, t, f.KeyGen()).id);
			revoke.add(new Bob(p, t, f.KeyGen()).id);
			a.FileEncrypt(message);
			P = f.aggregate(revoke);
			List<Proxy> ps = a.RkGen(P.get(P.size() - 1));
			for (int i = 0; i < reveiveNumber; i++) {
				Bob b1 = receiver.get(i);
				b1.getRekey(ps);
				b1.getEncryptedMessage();
				try {
					//System.out.println("Bob1:");
					b1.dec();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			//a.dec();
			long endTime = System.currentTimeMillis();    //获取结束时间
			//System.out.println("读写程序运行时间：" + time2 + "ms");    //输出程序运行时间
			System.out.print((endTime - startTime) + ",");    //输出程序运行时间
		}
	}

	void upLoadFile(byte[] file) {
		return;
	}
	
}